package com.atguigu.realtime.utils;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: 洛尘
 * @since: 2023-10-11 10:19
 * @description: 分词器工具类
 **/
public class KeywordUtil {

    public static  List<String> analyze(String text){
        List<String> keywordList = new ArrayList<>();
        try (StringReader reader = new StringReader(text)) {
            IKSegmenter segmenter = new IKSegmenter(reader, true);
            Lexeme lexeme;
            while ((lexeme = segmenter.next()) != null) {
                keywordList.add(lexeme.getLexemeText());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
//        for (int i = 0; i < keywordList.size(); i++) {
//            System.out.println(keywordList.get(i));
//        }
        return keywordList;
    }



    public static void main(String[] args) {
        String text = "我喜欢使用IK分词器进行中文分词。";
        analyze(text);
    }
}